package com.taru.controller;

import com.taru.model.Fruits;
import com.taru.model.Message;
import com.taru.service.MessageService;
import com.taru.utils.Constant;
import com.taru.utils.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@CrossOrigin
public class MessageController {
    private static Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    MessageService messageService;

    @RequestMapping("/insert/message")
    @ResponseBody
    public Object insertMessage(Message message){
        JsonResult result = null;
        try {
          int i =  messageService.insertMessage(message);
            if(i == 0){
                logger.info("添加", message.toString());
                result = new JsonResult("添加失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("添加", message.toString());
                result = new JsonResult("添加成功", Constant.STATUC_SUCCESS);
            }

        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("添加异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/update/message")
    @ResponseBody
    public Object updateMessageById(Message message){
        JsonResult result = null;
        try {
          int i =  messageService.updateMessageById(message);
            if(i == 0){
                logger.info("修改", message.toString());
                result = new JsonResult("修改失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("添加", message.toString());
                result = new JsonResult("修改成功", Constant.STATUC_SUCCESS);
            }
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("更新异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/delete/message")
    @ResponseBody
    public Object deleteMessageById(String messageId){
        JsonResult result = null;
        try {
           int i = messageService.deleteMessageById(messageId);
            if(i == 0){
                logger.info("删除", messageId);
                result = new JsonResult("删除失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("删除", messageId);
                result = new JsonResult("删除成功", Constant.STATUC_SUCCESS);
            }
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("删除异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/message/id")
    @ResponseBody
    public Object queryMessageById(@Param("fruitsBatch") String fruitsBatch, @Param("userId")  String userId){
        JsonResult result;
        try {
          List<Message> message =  messageService.queryMessageById(fruitsBatch,userId);
            logger.info("入参", userId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,message.get(0));
            logger.info("查询结果", message.toString());
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }


    @RequestMapping("/query/message/batch")
    @ResponseBody
    public Object queryMessageByBatch( @Param("userId")  String userId, @Param("ietm")  String ietm){
        JsonResult result;
        try {
            List<Message> messagelist =  messageService.queryMessageByBatch(userId, ietm);
            logger.info("入参",userId,ietm);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,messagelist);
            logger.info("查询结果", messagelist.toString());
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }
}
